<?php
/**
 * 发仔<i@fazi.me> 2018-07-17
 */

namespace app\open\controller;
/**
 * Class Plus
 * @name【8 统计】
 * @package app\open\controller
 */
use app\open\model;#调用MODEL
use app\open\validate\StatValidate;
use think\Db;

class Stat extends Base
{
    /**
     * 继承原来的adminapi
     */
    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * @name【1.1车辆统计】
     * @desc【车辆统计】
     * @method【post】
     * @table【p_car,p_car_type,p_maintenance_record,p_mot_record,p_refuel_record,p_car_insure】
     * @group【life】
     * @param【
     * car_id $ int $ 0 $ 车辆ID  #
     * company_id $ int $ 0 $ 公司ID  #
     * entrust_company_id $ int $ 0 $ 委托公司ID  #
     * prop $ int $ 0 $ 车辆性质  #
     * filter $ int $ 0 $ 筛选?  #
     * 】
     * @return【
     * code $ int $ number|1-1 $ 标识1-成功 0-失败#
     * msg $ string $  $ code=0，错误信息#
     * car_number $ int $  $ 汽车总数#
     * gongwuche_number $ int $  $ 公务车总数#
     * search_car_number $ int $  $ 搜索结果总数#
     * maintenance_record_number $ int $  $ 维保总额#
     * mot_record_number $ int $  $ 年检总额#
     * refuel_record_number $ int $  $ 加油总额#
     * car_insure_number $ int $  $ 保险总额#
     * oil_litre $ array $  $ 每月油量#
     * oil_price $ array $  $ 每月油费#
     * 】
     * @author 【yulin<570214321@qq.com>】
     */
    public function carStat(){

        $report = new StatValidate();
        $param = $report->scene('car_stat')->getCurrentData();
        $report->goCheck($param);
        $where = [];
        if($param['filter'] == 1){
            $where['is_beidou'] = ['=', "1"];
        } elseif($param['filter'] == 2) {
            $where['is_beidou'] = ['=', "1"];
            $where['entrust_company_id'] = ['=', "0"];
        }
        $param['car_id'] && $where['car_id'] = ['=', "%{$param['car_id']}%"];
        $param['company_id'] && $where['company_id'] = ['=', $param['company_id']];
        $param['entrust_company_id'] && $where['entrust_company_id'] = ['=', "%{$param['entrust_company_id']}%"];
        $param['prop'] && $where['prop'] = ['=', $param['prop']];
        //汽车总数
        $return_1 = (new model\Car())
            ->select();
        $data['car_number'] = count($return_1);

        //公务车总数
        $return_2 = (new model\CarType())
            ->where('type',2)
            ->field('car_type_id')
            ->select();
        $data['gongwuche_number'] = 0;
        foreach ($return_2 as $key2 => $val2) {
            $return_2_1 = (new model\Car())
                ->where('car_type_id',$return_2[$key2]['car_type_id'])
                ->select();
            $data['gongwuche_number'] += count($return_2_1);
        }

        //搜索结果总数
        $return_3 = (new model\Car())
            ->where($where)
            ->select();
        $data['search_car_number'] = count($return_3);


        $data['maintenance_record_number'] = 0;//维保总额
        $data['mot_record_number'] = 0;//年检总额
        $data['refuel_record_number'] = 0;//加油总额
        $data['car_insure_number'] = 0;//保险总额
        $data['oil_litre'] = array();//每月油量
        $data['oil_price'] = array();//每月油费


        //维保总额
        $data['maintenance_record_number'] = (new model\Car())
            ->alias('a')
            ->join('car_maintenance b','b.car_id = a.car_id')
            ->field('b.charge')
            ->where($where)
            ->sum('b.charge');

        //年检总额
        $data['mot_record_number'] = (new model\Car())
            ->alias('a')
            ->join('car_as b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->sum('b.price');

        //加油总额
        $data['refuel_record_number'] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->sum('b.price');

        //保险总额
        $data['car_insure_number'] = (new model\Car())
            ->alias('a')
            ->join('car_insurance b','b.car_id = a.car_id')
            ->field('b.policy_charge')
            ->where($where)
            ->sum('b.policy_charge');

        //每月加油金额
        $data['oil_litre'][1] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->where('start_date','like','%-01-%')
            ->sum('b.price');
        $data['oil_litre'][2] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->where('start_date','like','%-02-%')
            ->sum('b.price');
        $data['oil_litre'][3] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->where('start_date','like','%-03-%')
            ->sum('b.price');
        $data['oil_litre'][4] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->where('start_date','like','%-04-%')
            ->sum('b.price');
        $data['oil_litre'][5] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->where('start_date','like','%-05-%')
            ->sum('b.price');
        $data['oil_litre'][6] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->where('start_date','like','%-06-%')
            ->sum('b.price');
        $data['oil_litre'][7] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->where('start_date','like','%-07-%')
            ->sum('b.price');
        $data['oil_litre'][8] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->where('start_date','like','%-08-%')
            ->sum('b.price');
        $data['oil_litre'][9] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->where('start_date','like','%-09-%')
            ->sum('b.price');
        $data['oil_litre'][10] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->where('start_date','like','%-10-%')
            ->sum('b.price');
        $data['oil_litre'][11] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->where('start_date','like','%-11-%')
            ->sum('b.price');
        $data['oil_litre'][12] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.price')
            ->where($where)
            ->where('start_date','like','%-12-%')
            ->sum('b.price');

        //每月加油量
        $data['oil_price'][1] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.litre')
            ->where($where)
            ->where('start_date','like','%-01-%')
            ->sum('b.litre');
        $data['oil_price'][2] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.litre')
            ->where($where)
            ->where('start_date','like','%-02-%')
            ->sum('b.litre');
        $data['oil_price'][3] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.litre')
            ->where($where)
            ->where('start_date','like','%-03-%')
            ->sum('b.litre');
        $data['oil_price'][4] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.litre')
            ->where($where)
            ->where('start_date','like','%-04-%')
            ->sum('b.litre');
        $data['oil_price'][5] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.litre')
            ->where($where)
            ->where('start_date','like','%-05-%')
            ->sum('b.litre');
        $data['oil_price'][6] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.litre')
            ->where($where)
            ->where('start_date','like','%-06-%')
            ->sum('b.litre');
        $data['oil_price'][7] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.litre')
            ->where($where)
            ->where('start_date','like','%-07-%')
            ->sum('b.litre');
        $data['oil_price'][8] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.litre')
            ->where($where)
            ->where('start_date','like','%-08-%')
            ->sum('b.litre');
        $data['oil_price'][9] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.litre')
            ->where($where)
            ->where('start_date','like','%-09-%')
            ->sum('b.litre');
        $data['oil_price'][10] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.litre')
            ->where($where)
            ->where('start_date','like','%-10-%')
            ->sum('b.litre');
        $data['oil_price'][11] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.litre')
            ->where($where)
            ->where('start_date','like','%-11-%')
            ->sum('b.litre');
        $data['oil_price'][12] = (new model\Car())
            ->alias('a')
            ->join('refuel_record b','b.car_id = a.car_id')
            ->field('b.litre')
            ->where($where)
            ->where('start_date','like','%-12-%')
            ->sum('b.litre');
        for($i=1;$i<=12;$i++){
            $data['oil_litre'][$i] = round($data['oil_litre'][$i] ,2);;
            $data['oil_price'][$i] = round($data['oil_litre'][$i] ,2);;
        }

        return $this->res($data);
    }

    /**
     * @name【2.1订单统计】
     * @desc【订单统计】
     * @method【post】
     * @table【p_order,p_order_fee】
     * @group【life】
     * @param【
     * start_time $ string $ 2018-7-25 $ 开始时间  #
     * end_time $ string $ 2018-7-25 $ 结束时间  #
     * day $ int $ 0 $ 1:查询今天  #
     * month $ int $ 0 $ 1:查询本月  #
     * year $ int $ 0 $ 1:查询今年  #
     * 】
     * @return【
     * code $ int $ number|1-1 $ 标识1-成功 0-失败#
     * msg $ string $  $ code=0，错误信息#
     * order_money $ array/float $  $ 金额#
     * order_money_percent $ array/float $  $ 金额的百分比#
     * order_money_total $ float $  $ 总金额#
     * order_number $ array $  $ 次数#
     * order_number_percent $ array/float $  $ 次数的百分比#
     * order_number_total $ float $  $ 总次数#
     * order_time $ array $  $ 时间#
     * order_time_percent $ array/float $  $ 时间的百分比#
     * order_time_total $ float $  $ 总时间#
     * 】
     * @author 【yulin<570214321@qq.com>】
     */
    public function orderStat(){

        $report = new StatValidate();
        $param = $report->scene('order_stat')->getCurrentData();
        $report->goCheck($param);
        $where = [];

        if($param['start_time'] || $param['end_time']) {
            $param['start_time'] && $where['order_time'] = ['> time', "{$param['start_time']}"];
            $param['end_time'] && $where['order_time'] = ['<= time', "{$param['end_time']}"];
            if ($param['start_time'] && $param['end_time']) {
                $where['order_time'] = ['between time', [$param['start_time'], $param['end_time']]];
            }
            $list = (new model\Order())
                ->where($where)
                ->alias('a')
                ->join('order_fee b','a.order_id=b.order_id')
                ->select();
//            $list = Db::name('order')
//                ->where($where)
//                ->alias('a')
//                ->join('order_fee b','a.order_id=b.order_id')
//                ->select();
        }
        else {
            if($param['day']){
                $list =  (new model\Order())
                    ->whereTime('order_time', 'today')
                    ->alias('a')
                    ->join('order_fee b','a.order_id=b.order_id')
                    ->select();
            }
            elseif($param['month']){
                $list =  (new model\Order())
                    ->whereTime('order_time', 'month')
                    ->alias('a')
                    ->join('order_fee b','a.order_id=b.order_id')
                    ->select();
            }
            else{
                $list =  (new model\Order())
                    ->whereTime('order_time', 'year')
                    ->alias('a')
                    ->join('order_fee b','a.order_id=b.order_id')
                    ->select();
            }
        }

        $data['order_number'] = [];
        $data['order_money'] = [];
        $data['order_time'] = [];
        $data['order_number_percent'] = [];
        $data['order_money_percent'] = [];
        $data['order_time_percent'] = [];
        $data['order_number_total'] = 0;
        $data['order_money_total'] = 0;
        $data['order_time_total'] = 0;

        for($i=1;$i<=8;$i++){
            $data['order_number'][$i] = 0;
            $data['order_money'][$i] = 0;
            $data['order_time'][$i] = 0;
            $data['order_number_percent'][$i] = 0;
            $data['order_money_percent'][$i] = 0;
            $data['order_time_percent'][$i] = 0;
        }

        foreach ($list as $key => $val) {
            $data['order_number'][$list[$key]['use_type']] += 1;
            $data['order_number_total'] += 1;
            $data['order_money'][$list[$key]['use_type']] += $list[$key]['order_money'];
            $data['order_money_total'] += $list[$key]['order_money'];
            $data['order_time'][$list[$key]['use_type']] += $list[$key]['use_time'];
            $data['order_time_total'] += $list[$key]['use_time'];
        }

        for($i=1;$i<=8;$i++){
            if($data['order_number_total'] > '0') {
                $data['order_number_percent'][$i] = $data['order_number'][$i] / $data['order_number_total'];
            }
            else{
                $data['order_number_percent'][$i] = 0;
            }
            if($data['order_money_total'] > '0') {
                $data['order_money_percent'][$i] = $data['order_money'][$i] / $data['order_money_total'];
            }
            else{
                $data['order_money_percent'][$i] = 0;
            }
            if($data['order_time_total'] > '0') {
                $data['order_time_percent'][$i] = $data['order_time'][$i] / $data['order_time_total'];
            }
            else{
                $data['order_time_percent'][$i] = 0;
            }

            $data['order_number'][$i] = round($data['order_number'][$i] ,2);
            $data['order_money'][$i] = round($data['order_money'][$i] ,2);
            $data['order_time'][$i] = round($data['order_time'][$i] ,2);

            $data['order_number_percent'][$i] = round($data['order_number_percent'][$i]*100 ,2);
            $data['order_money_percent'][$i] = round($data['order_money_percent'][$i]*100 ,2);
            $data['order_time_percent'][$i] = round($data['order_time_percent'][$i]*100 ,2);
        }

        $data['order_number_total'] = round($data['order_number_total'] ,2);
        $data['order_money_total'] = round($data['order_money_total'] ,2);
        $data['order_time_total'] = round($data['order_time_total'] ,2);

        return $this->res($data);
    }

    /**
     * @name【3.1司机统计】
     * @desc【司机统计】
     * @method【post】
     * @table【p_order,p_order_fee】
     * @group【life】
     * @param【
     * driver_id $ int $ 11 $ 司机ID  #
     * company_id $ int $ 109 $ 公司ID  #
     * all $ int $ 1 $ 全部  #
     * day $ int $ 0 $ 今日  #
     * month $ int $ 0 $ 本月  #
     * year $ int $ 0 $ 今年  #
     * 】
     * @return【
     * code $ int $ number|1-1 $ 标识1-成功 0-失败#
     * msg $ string $  $ code=0，错误信息#
     * all_meter $ int $  $ 总里程#
     * all_meter_ex $ int $  $ 前`总里程#
     * all_time $ int $  $ 总在线时长#
     * all_time_ex $ int $  $ 前`总在线时长#
     * bad_evaluate $ int $  $ 投诉#
     * bad_evaluate_ex $ int $  $ 前`投诉#
     * cancel $ int $  $ 取消#
     * cancel_ex $ int $  $ 前`取消#
     * check $ int $  $ 接单数#
     * check_ex $ int $  $ 前`接单数#
     * evaluate $ int $  $ 评价#
     * evaluate_ex $ int $  $ 前`评价#
     * money_water $ int $  $ 流水#
     * money_water_ex $ int $  $ 前`流水#
     * no_onlie $ int $  $ 不在线司机数量#
     * number $ int $  $ 司机数量#
     * onlie $ int $  $ 在线数量#
     * 】
     * @author 【yulin<570214321@qq.com>】
     */
    public function driverStat(){

        $report = new StatValidate();
        $param = $report->scene('driver_stat')->getCurrentData();
        $report->goCheck($param);
        $where = [];
        $where_c = [];

        $data['number'] = 0;
        $data['onlie'] = 0;
        $data['all_time'] = 0;
        $data['all_meter'] = 0;
        $data['money_water'] = 0;
        $data['check'] = 0;
        $data['cancel'] = 0;
        $data['bad_evaluate'] = 0;
        $data['evaluate'] = 0;

        $data['no_onlie'] = 0;

        $data['all_time_ex'] = 0;
        $data['all_meter_ex'] = 0;
        $data['money_water_ex'] = 0;
        $data['check_ex'] = 0;
        $data['cancel_ex'] = 0;
        $data['bad_evaluate_ex'] = 0;
        $data['evaluate_ex'] = 0;

        $param['driver_id'] && $where['driver_id'] = ['=', "{$param['driver_id']}"];
        $param['company_id'] && $where['company_id'] = ['=', "{$param['company_id']}"];

        $param['driver_id'] && $where_c['c.driver_id'] = ['=', "{$param['driver_id']}"];
        $param['company_id'] && $where_c['c.company_id'] = ['=', "{$param['company_id']}"];

        $time = 'year';
        $time_ex = 'last year';
        $param['day'] && $time = 'today';
        $param['day'] && $time = 'yesterday';
        $param['month'] && $time = 'month';
        $param['month'] && $time = 'last month';
        $param['year'] && $time = 'year';
        $param['year'] && $time = 'last year';
        $param['all'] && $time = 'year';
        $param['all'] && $time = 'last year';

        $data['number'] = (new model\Driver())
            ->where($where)
            ->where('work_state','in','1,2,3,4')
            ->field('')
            ->count('work_state');
        $data['onlie'] = (new model\Driver())
            ->where($where)
            ->where('work_state','in','1,2,3')
            ->field('')
            ->count('work_state');
        $data['no_onlie'] = (new model\Driver())
            ->where($where)
            ->where('work_state','in','4')
            ->field('')
            ->count('work_state');

        $order_return =  (new model\Order())
            ->alias('a')
            ->join('order_fee b','a.order_id=b.order_id')
            ->join('driver c','c.driver_id=a.driver_id')
            ->whereTime('order_time', $time)
            ->where($where_c)
            ->field('time,meter,order_money,cancel_time,proposal,evaluate')
            ->select();

        $order_return_ex =  (new model\Order())
            ->alias('a')
            ->join('order_fee b','a.order_id=b.order_id')
            ->join('driver c','c.driver_id=a.driver_id')
            ->whereTime('order_time', $time_ex)
            ->where($where_c)
            ->field('time,meter,order_money,cancel_time,proposal,evaluate')
            ->select();

        foreach ($order_return as $key => $val) {
            $data['all_time'] += $order_return[$key]['time'];
            $data['all_meter'] += $order_return[$key]['meter'];
            $data['money_water'] += $order_return[$key]['order_money'];
            if($order_return[$key]['cancel_time'] > 0){
                $data['cancel'] += 1;
            }
            else{
                $data['check'] += 1;
            }
            if($order_return[$key]['proposal']){
                $data['bad_evaluate'] += 1;
            }
            if($order_return[$key]['evaluate']){
                $data['evaluate'] += 1;
            }
        }

        foreach ($order_return_ex as $key => $val) {
            $data['all_time_ex'] += $order_return_ex[$key]['time'];
            $data['all_meter_ex'] += $order_return_ex[$key]['meter'];
            $data['money_water_ex'] += $order_return_ex[$key]['order_money'];
            if($order_return_ex[$key]['cancel_time'] > 0){
                $data['cancel_ex'] += 1;
            }
            else{
                $data['check_ex'] += 1;
            }
            if($order_return_ex[$key]['proposal']){
                $data['bad_evaluate_ex'] += 1;
            }
            if($order_return_ex[$key]['evaluate']){
                $data['evaluate_ex'] += 1;
            }

        }

        $data['all_meter'] = round($data['all_meter'] ,0);
        $data['all_meter_ex'] = round($data['all_meter_ex'] ,0);
        $data['money_water'] = round($data['money_water'] ,0);
        $data['money_water_ex'] = round($data['money_water_ex'] ,0);

        return $this->res($data);
    }
}